unit uretimdurum;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, StdCtrls,
  ExtCtrls, DBCtrls, Mask, wwdbedit, Db, ADODB, wwdbdatetimepicker, Grids,
  Wwdbigrd, Wwdbgrid, variants, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGrid, wwdblook, Wwdotdot, Wwdbcomb, Buttons,
  unitOrtak, cxDBLookupComboBox;

type
  TFuretimdurum = class(TForm)
    StatusBar1: TStatusBar;
    UretimDurumQ: TADOQuery;
    UretimDurumQS: TDataSource;
    Panel2: TPanel;
    Label7: TLabel;
    Label8: TLabel;
    t2: TwwDBDateTimePicker;
    t1: TwwDBDateTimePicker;
    Panel1: TPanel;
    bYenile: TButton;
    gostermek: TCheckBox;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGrid1DBTableView1Tarih: TcxGridDBColumn;
    cxGrid1DBTableView1emirno: TcxGridDBColumn;
    cxGrid1DBTableView1stokkodu: TcxGridDBColumn;
    cxGrid1DBTableView1cinsi: TcxGridDBColumn;
    cxGrid1DBTableView1miktar: TcxGridDBColumn;
    Label3: TLabel;
    cxGrid1DBTableView1mdagitim: TcxGridDBColumn;
    cxGrid1DBTableView1montaj: TcxGridDBColumn;
    cxGrid1DBTableView1test: TcxGridDBColumn;
    cxGrid1DBTableView1sonkontrol: TcxGridDBColumn;
    cxGrid1DBTableView1paket: TcxGridDBColumn;
    cxGrid1DBTableView1kalan: TcxGridDBColumn;
    cxGrid1DBTableView1depo: TcxGridDBColumn;
    s1: TStaticText;
    s2: TStaticText;
    s3: TStaticText;
    s4: TStaticText;
    s5: TStaticText;
    s6: TStaticText;
    depok: TwwDBLookupCombo;
    Label11: TLabel;
    AmbarUretimQ: TADOQuery;
    bayrintigor: TBitBtn;
    ts2: TStaticText;
    ts3: TStaticText;
    ts4: TStaticText;
    ts5: TStaticText;
    ts6: TStaticText;
    Label1: TLabel;
    Label2: TLabel;
    bkapat: TBitBtn;
    adoTur: TADOQuery;
    lcTur: TwwDBLookupCombo;
    cxGrid1DBTableView1Model: TcxGridDBColumn;
    cxGrid1DBTableView1AnaStokkodu: TcxGridDBColumn;
    cxGrid1DBTableView1CariGrup: TcxGridDBColumn;
    aqStokCari: TADOQuery;
    dsStokCari: TDataSource;
    Button1: TButton;
    Emirno: TEdit;
    Label4: TLabel;
    sp_ihale: TADOStoredProc;
    cxGrid1DBTableView1Merkez: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure bYenileClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure cxGrid1DBTableView1DblClick(Sender: TObject);
    procedure bayrintigorClick(Sender: TObject);
    procedure bkapatClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Furetimdurum: TFuretimdurum;

implementation

uses data, UretimAcilimi, ana1;

{$R *.DFM}

procedure TFuretimdurum.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  action := cafree;
  Furetimdurum := nil;
end;

procedure TFuretimdurum.bYenileClick(Sender: TObject);
var
  sayi1, sayi2: integer;
  i: integer;
  depo: string;
begin
  if LcTur.Text = '' then
    depo := '%'
  else
    depo := depok.LookupValue;

  UretimDurumQ.DisableControls;

  UretimDurumQ.Close;
  UretimDurumQ.SQL.Text := 'select Sec=1, UstGrupId=(select UstGrupId from stok where stok.stokkodu=uremri.stokkodu),GrupID=(select GrupId from stok where stok.stokkodu=uremri.stokkodu),tarih,emirno,stokkodu,cinsi,miktar,';
  UretimDurumQ.SQL.Add('AnaStokkodu=(Select Stokkodu From Stok Where Stokkod=(select AnaStokkod from stok where stok.stokkodu=uremri.stokkodu)), ');
  UretimDurumQ.SQL.Add('CariGrup=(Select CariGrup from Stok where stok.stokkodu=uremri.stokkodu), ');
  UretimDurumQ.SQL.Add('ModelId=(select ModelId from stok where stok.stokkodu=uremri.stokkodu), ');
  UretimDurumQ.SQL.Add('depo=(case when durumu is null then 0 else miktar end),mdagitim=(select count(makinano) from uretim WITH(NOLOCK,INDEX(Uretimno)) where uretim.uretimno=uremri.emirno), ');
  UretimDurumQ.SQL.Add('montaj=(select count(makinano) from uretim WITH(NOLOCK,INDEX(Uretimno)) where uretim.uretimno=uremri.emirno and (uretim.montajtarih is not null or uretim.paktar is not null)),');
  UretimDurumQ.SQL.Add('test=(select count(makinano) from uretim WITH(NOLOCK,INDEX(Uretimno)) where uretim.uretimno=uremri.emirno and (testt is not null or uretim.paktar is not null)),');
  UretimDurumQ.SQL.Add('sonkontrol=(select count(makinano) from uretim WITH(NOLOCK,INDEX(Uretimno)) where uretim.uretimno=uremri.emirno and (sontariht is not null or uretim.paktar is not null)),');
  UretimDurumQ.SQL.Add('Merkez=(select count(makinano) from uretim WITH(NOLOCK,INDEX(Uretimno)) where uretim.uretimno=uremri.emirno and (MerkezTar is not null)),');
  UretimDurumQ.SQL.Add('paket=(select count(makinano) from uretim WITH(NOLOCK,INDEX(Uretimno)) where uretim.uretimno=uremri.emirno and paktar is not null),');
  UretimDurumQ.SQL.Add('kalan=miktar-(select count(makinano) from uretim WITH(NOLOCK,INDEX(Uretimno)) where uretim.uretimno=uremri.emirno and paktar is not null) from uremri WITH(NOLOCK,INDEX(Tarih)) ');
  UretimDurumQ.SQL.Add('where tarih>=' + #39 + TarihDonustur(t1.date, srvCasper, '') + #39 + ' and tarih<' + #39 + TarihDonustur(t2.date + 1, srvCasper, '') + #39);
  UretimDurumQ.SQL.Add('and isnull(Uremri.Ozelkod1,'''') not in (Select Adi from TutanakTanim Where UretimdeGosterme=1)');

  if depok.text <> '' then
    UretimDurumQ.SQL.Add(' and ciktidepo=' + depok.lookupvalue);

  UretimDurumQ.SQL.Add(' order by tarih,emirno');
  UretimDurumQ.Open;

  UretimDurumQ.Filter := 'Sec=1 ';

  if (gostermek.Checked) or (LcTur.Text <> '') then
  begin
    if (gostermek.Checked) then
      UretimDurumQ.Filter := UretimDurumQ.Filter + ' AND Kalan<>0';

    if (LcTur.Text <> '') then
    begin
      if adoTur.Locate('Ref', LcTur.LookupValue, []) then
      begin
        if AdoTur.FieldByName('UstGrupId').AsString <> '' then
          UretimDurumQ.Filter := UretimDurumQ.Filter + ' AND UstGrupId=' + AdoTur.FieldByName('UstGrupId').AsString;

        if AdoTur.FieldByName('GrupId').AsString <> '' then
          UretimDurumQ.Filter := UretimDurumQ.Filter + ' AND GrupId=' + AdoTur.FieldByName('GrupId').AsString;
      end;
    end;

    UretimDurumQ.Filtered := True;
  end
  else
    UretimDurumQ.Filtered := False;

  UretimDurumQ.EnableControls;

  if UretimDurumQ.RecordCount = 0 then exit;

  for i := 0 to 5 do
  begin
    if cxGrid1DBTableView1.DataController.Summary.FooterSummaryValues[i] = 0
      then
      sayi1 := 0
    else
      sayi1 := strtoint(cxGrid1DBTableView1.DataController.Summary.FooterSummaryValues[i]);

    if cxGrid1DBTableView1.DataController.Summary.FooterSummaryValues[i + 1] = 0
      then
      sayi2 := 0
    else
      sayi2 :=
        strtoint(cxGrid1DBTableView1.DataController.Summary.FooterSummaryValues[i
        + 1]);

    (FindComponent('s' + inttostr(i + 1)) as tStaticText).Caption :=
      inttostr(sayi1 - sayi2);
  end;

  ts2.Caption := inttostr(strtoint(s1.Caption) + strtoint(s2.Caption));
  ts3.Caption := inttostr(strtoint(ts2.Caption) + strtoint(s3.Caption));
  ts4.Caption := inttostr(strtoint(ts3.Caption) + strtoint(s4.Caption));
  ts5.Caption := inttostr(strtoint(ts4.Caption) + strtoint(s5.Caption));
  ts6.Caption := inttostr(strtoint(ts5.Caption) + strtoint(s6.Caption));
end;


procedure TFuretimdurum.FormShow(Sender: TObject);
begin
  YerAyarla(Self, false);

  t1.Date := SadeceTarih - 1;
  t2.Date := SadeceTarih;

  AdoTur.open;
  AmbarUretimQ.open;
  depok.lookupvalue := '2';

  lcTur.LookupValue := '1';

//  bYenile.Click;
end;

procedure TFuretimdurum.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = vk_F5 then
    byenile.click;
end;

procedure TFuretimdurum.cxGrid1DBTableView1DblClick(Sender: TObject);
begin
  bayrintigor.click;
end;

procedure TFuretimdurum.bayrintigorClick(Sender: TObject);
begin
  if UretimDurumQ.RecNo > 0 then
  begin
    UretimAcilimiF := tUretimAcilimiF.create(Anaform);
    UretimAcilimiF.UretimNO := inttostr(UretimDurumQ.fieldbyname('EmirNo').asinteger);
    UretimAcilimiF.FormStyle := FSMdiChild;
  end;
end;

procedure TFuretimdurum.bkapatClick(Sender: TObject);
begin
  close;
end;


procedure TFuretimdurum.FormCreate(Sender: TObject);
begin
  aqStokCari.Open;
end;

procedure TFuretimdurum.Button1Click(Sender: TObject);
begin
//  sp_ihale.Close;
//  sp_ihale.Parameters.ParamByName('@Emirno').Value := Emirno.Text;
//  sp_ihale.ExecProc;
end;

end.

